home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / comm / mail / YAM23src.lha / Source / hmac_md5.c < prev    next >
C/C++ Source or Header  |  2001-05-08  |  4KB  |  101 lines

  1. /***************************************************************************
  2.  
  3.  YAM - Yet Another Mailer
  4.  Copyright (C) 1995-2000 by Marcel Beck <mbeck@yam.ch>
  5.  Copyright (C) 2000-2001 by YAM Open Source Team
  6.  
  7.  This program is free software; you can redistribute it and/or modify
  8.  it under the terms of the GNU General Public License as published by
  9.  the Free Software Foundation; either version 2 of the License, or
  10.  (at your option) any later version.
  11.  
  12.  This program is distributed in the hope that it will be useful,
  13.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  GNU General Public License for more details.
  16.  
  17.  You should have received a copy of the GNU General Public License
  18.  along with this program; if not, write to the Free Software
  19.  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20.  
  21.  YAM Official Support Site :  http://www.yam.ch
  22.  YAM OpenSource project    :  http://sourceforge.net/projects/yamos/
  23.  
  24.  $Id: hmac_md5.c,v 1.7 2001/05/08 22:27:37 damato Exp $
  25.  
  26. ***************************************************************************/
  27.  
  28. #include <md5.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31.  
  32. void hmac_md5(unsigned char * text, int text_len, unsigned char *key, int key_len, unsigned char digest[16])
  33. /* text     pointer to data stream */
  34. /* text_len length of data stream */
  35. /* key      pointer to authentication key */
  36. /* key_len  length of authentication key */
  37. /* digest   caller digest to be filled in */
  38. {
  39.         MD5_CTX context;
  40.         unsigned char k_ipad[65];    /* inner padding -
  41.                                       * key XORd with ipad
  42.                                       */
  43.         unsigned char k_opad[65];    /* outer padding -
  44.                                       * key XORd with opad
  45.                                       */
  46.         unsigned char tk[16];
  47.         int i;
  48.         /* if key is longer than 64 bytes reset it to key=MD5(key) */
  49.         if (key_len > 64) {
  50.  
  51.                 MD5_CTX      tctx;
  52.  
  53.                 MD5Init(&tctx);
  54.                 MD5Update(&tctx, key, key_len);
  55.                 MD5Final(tk, &tctx);
  56.  
  57.                 key = tk;
  58.                 key_len = 16;
  59.         }
  60.  
  61.         /*
  62.          * the HMAC_MD5 transform looks like:
  63.          *
  64.          * MD5(K XOR opad, MD5(K XOR ipad, text))
  65.          *
  66.          * where K is an n byte key
  67.          * ipad is the byte 0x36 repeated 64 times
  68.          * opad is the byte 0x5c repeated 64 times
  69.          * and text is the data being protected
  70.          */
  71.  
  72.         /* start out by storing key in pads */
  73.         bzero( k_ipad, sizeof k_ipad);
  74.         bzero( k_opad, sizeof k_opad);
  75.         bcopy( key, k_ipad, key_len);
  76.         bcopy( key, k_opad, key_len);
  77.  
  78.         /* XOR key with ipad and opad values */
  79.         for (i=0; i<64; i++) {
  80.                 k_ipad[i] ^= 0x36;
  81.                 k_opad[i] ^= 0x5c;
  82.         }
  83.         /*
  84.          * perform inner MD5
  85.          */
  86.         MD5Init(&context);                   /* init context for 1st
  87.                                               * pass */
  88.         MD5Update(&context, k_ipad, 64);     /* start with inner pad */
  89.         MD5Update(&context, text, text_len); /* then text of datagram */
  90.         MD5Final(digest, &context);          /* finish up 1st pass */
  91.         /*
  92.          * perform outer MD5
  93.          */
  94.         MD5Init(&context);                   /* init context for 2nd
  95.                                               * pass */
  96.         MD5Update(&context, k_opad, 64);     /* start with outer pad */
  97.         MD5Update(&context, digest, 16);     /* then results of 1st
  98.                                               * hash */
  99.         MD5Final(digest, &context);          /* finish up 2nd pass */
  100. }
  101.